(function () {
    var as = angular.module('exampleApp.controllers');

    as.controller('OffreController', function ($scope, $http, i18n) {
        $scope.p = 1;
        $scope.q = '';

        var actionUrl = 'api/offres/',
                load = function () {
                    $http.get(actionUrl + '?page=' + ($scope.p - 1))
                            .success(function (data) {
                                $scope.offres = data.content;
                                $scope.totalItems = data.totalElements;
                            });
                };

        load();
        
        $scope.search = function () {
            load();
        };
        
        $scope.initAdd = function () {
            $scope.produit = {};
            $('#offreDialog').modal('show');
        };

        $scope.save = function () {
            $http.post(actionUrl, $scope.offre).success(function () {
                $('#offreDialog').modal('hide');
                load();
            });
        };

        $scope.delProduit = function (idx) {
            console.log('delete index @' + idx + ', id is@' + $scope.users[idx].id);
            if (confirm($.i18n.prop('confirm.delete'))) {
                $http.delete(actionUrl + $scope.posts[idx].id)
                        .success(function () {
                            $scope.posts.splice(idx, 1);
                        });
            }
        };

    });
    
    as.controller('OffreDetailsController', function ($scope, $http, $routeParams, $q) {
        $scope.p = 1;
        var actionUrl = 'api/offres/',
                loadOffre = function () {
                    $http.get(actionUrl + $routeParams.id)
                            .success(function (data) {
                                $scope.montant = data.montant;
                                $scope.dateOffre = new Date(data.dateOffre);
                                $scope.statut = data.statut;
                                $scope.produit = data.produitDto;
                                $scope.acheteur = data.acheteur;
                            });
                }

        loadOffre();
        
        $scope.showProduit = function () {
        	$('#offreProduitPanel').modal('show');
        }


        $scope.save = function () {
            $http.post(actionUrl + $routeParams.id , $scope.produit).success(function () {
                $('#offreProduitPanel').modal('hide');
                loadOffre();
//                $scope.newProduit = {};
            });
        };

        $scope.delComment = function (idx) {
            $http.delete('api/comments/' + $scope.comments[idx].id).success(function () {
                $scope.comments.splice(idx, 1);
            });
        };

        $scope.addComment = function () {
            $('#commentDialog').modal('show');
        };

        $scope.search = function () {
            loadComments();
        };

    });

}());